package com.houmeiyuan.huiguniang.ctrl;

import com.houmeiyuan.huiguniang.common.MessageConstVar;
import com.houmeiyuan.huiguniang.common.ParamsUtil;
import com.houmeiyuan.huiguniang.common.Result;
import com.houmeiyuan.huiguniang.modles.domain.Address;
import com.houmeiyuan.huiguniang.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressCtrl extends BaseCtrl{
    @Autowired
    public AddressService addressService;

    /**
     * @author fanfan 2019/8/14
     * 通过用户的openId以及id获取收获地址  当id为-1时获取所有收获地址
     * @param pageStart
     * @param pageSize
     * @param openId  用户openId
     * @param id 地址id
     * @return
     */
    @GetMapping("/fetch")
   public Result fetchByAddressId(
        @RequestParam(name = "pageStart", defaultValue = "1") Integer pageStart,
        @RequestParam(name = "pageSize", defaultValue = "7") Integer pageSize,
        @RequestParam(value = "openId") String openId ,
        @RequestParam(value="id" ,defaultValue="-1" ) Integer id)
    {
        Map<String, Object> mp = new HashMap<>();
        mp.put("openId",openId);
        mp.put("id",id);
        mp = ParamsUtil.addPageInfo(mp, pageStart, pageSize);
        //id为正只查询一条地址记录  id为-1 查询该用户的所有收货地址
        if(id!=-1)
        {
            Address address=addressService.fetchByAddressId(mp);
            System.out.println(address);
            if(address==null)
                return send(MessageConstVar.ErrorCode,MessageConstVar.Error);
            else
              return send(address);
        }
        else{

            List list=addressService.fetchAll(mp);
            //如果list未分配内存或者list为空
            if(list==null||list.isEmpty()==true)
                return send(MessageConstVar.ErrorCode,MessageConstVar.Error);
            else
              return send(list);
        }


    }

    /**
     * @author fanfan 2019/8/19
     * 添加收件地址
     * @param address
     * @return
     */
    @PostMapping("/add")
    public Result addAddress(@Valid  Address address)
    {
        Result result=new Result();
        Integer res=addressService.addAddress(address);
        if(res==1)
            result.setResult(1);
        else
            result.setResult(0);
         return result;
    }


    /**
     * 编辑更新地址
     *
     * @param address
     * @return
     * @Author nkz
     * @RequestBody(required = false)不能加
     */
    @PostMapping("/update")
    public Result update(Address address){
        try{
            return send(addressService.update(address),0,"更新成功!");
        }catch (Exception e){
            e.printStackTrace();
            return send(1,"更新失败!");
        }
    }

    /**
     * 删除指定id的收货地址
     * @author fanfan 2019/8/20
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteAddress(@RequestParam(value = "id") Integer id){
        Result res=new Result();
        try{
            Integer count = addressService.deleteAddress(id);
            if(count !=0){
                res.setStatus(MessageConstVar.SuccessCode,MessageConstVar.Success);
                res.setResult(1);
                return res;
            }
            res.setResult(0);
            return send(MessageConstVar.SystemcErrCode,MessageConstVar.SystemcErr);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return send(MessageConstVar.ErrorCode,MessageConstVar.Error);
        }

    }

}
